package com.cssw.fyzb.wap.service;

import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.ydn.appserver.Response;

/**
 * @author Feng Chen
 */
public abstract class BaseService {

    protected <T> AjaxData<T> convertData(Response response, Class<T> type) {
        AjaxData result = null;
        if (response.isSuccess()) {
            result = AjaxData.success(response.getData().get("data", type));
        } else {
            result = AjaxData.failure(response.getCode(), response.getMessage());
        }
        return result;
    }

    protected <T> AjaxPage<T> convertPage(Response response, Class<T> type) {
        AjaxPage result = null;
        if (response.isSuccess()) {
            Integer total = response.getInteger("total");
            if (total == null){
                total = 0;
            }
            Integer count = response.getInteger("count");
            if (count == null){
                count = 0;
            }
            result = AjaxPage.success(response.getList("list", type), total, count);
        } else {
            result = AjaxPage.failure(response.getCode(), response.getMessage());
        }
        return result;
    }

    protected AjaxData convertData(Response response) {
        AjaxData result = null;
        if (response.isSuccess()) {
            result = AjaxData.success(response.getData().get("data"));
        } else {
            result = AjaxData.failure(response.getCode(), response.getMessage());
        }
        return result;
    }
}
